package com.woniu.springbootrbac.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.springbootrbac.dto.DeptDto;
import com.woniu.springbootrbac.entity.SysDept;
import com.woniu.springbootrbac.service.DeptService;
import com.woniu.springbootrbac.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService service;

    @PostMapping("selAll")
    public Result selAll(@RequestBody DeptDto dept){
        PageHelper.startPage(dept.getCurrentPage(),dept.getPageSize(),true);
        List<SysDept> sysDepts = service.selAll(dept);
        PageInfo<SysDept> pageInfo = new PageInfo<>(sysDepts);
        return Result.ok().data("list",sysDepts).data("total",pageInfo.getTotal());
    }

    @PostMapping("")
    public Result add(@RequestBody SysDept dept){
        int i = service.insertSelective(dept);
        return Result.ok();
    }

    @GetMapping("{id}")
    public Result selOne(@PathVariable Long id){
        SysDept sysDept = service.selectByPrimaryKey(id);
        return Result.ok().data("dept",sysDept);
    }

    @PutMapping("")
    public Result update(@RequestBody SysDept dept){
        int i = service.updateByPrimaryKeySelective(dept);
        return Result.ok();
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable Long id){
        int i = service.deleteByPrimaryKey(id);
        if(i == 0){
            return Result.error();
        } else {
            return Result.ok();
        }
    }

}
